//
// Created by Administrator on 2022/1/10.
//

#include <stdio.h>
#include <stdlib.h>

// 1:注意*的优先级没有()高
int *(f1(int, double));

// 2:f2为指针类型
int (*f2)(int, double);

// 3:返回类型是一个int*指针(整形的指针)
int *(*f3)(int, double);

// 4:int 数组[],不合法
//int (*f)(int, double)[];
//int ((*f)(int, double))[];

// 5:函数的数组,不合法
//int (*f5)[](int, double);//不合法
//int ((*f5))[](int, double);//不合法
//int (*f5[])(int, double);//合法,f5是一个指针类型的数组，指针类型为int (*f5[])(int, double)函数类型的指针，类比于【void (*func)(int **ptr, int length, int default_value) =&InitPointer;】

//为类型取别名
typedef int (*Func)(int, double);

int Add(int left, double right) {
    return left + (int) right;
}

void InitPointer(int **ptr, int length, int default_value) {
    *ptr = malloc(sizeof(int) * length);
    for (int i = 0; i < length; ++i) {
        (*ptr)[i] = default_value;
    }
}

int main() {
    int a;
    int *p;

    void (*func)(int **ptr, int length, int default_value) =&InitPointer;
    func(&p, 10, 0);
    for (int i = 0; i < 10; ++i) {
        printf("%d,", p[i]);
    }
    printf("\n%p\n", &InitPointer);
    printf("%p\n", &main);
    //深思熟虑
    func(&p, 10, 0);
    InitPointer(&p, 10, 0);
    (*func)(&p, 10, 0);
    //Redundant repeated dereference of function pointer
    //函数指针的冗余重复取消引用
    (*InitPointer)(&p, 10, 0);

    //typedef Func的使用
    Func func1 = &Add;
    int sum = func1(1, 2.1);
    printf("%d\n", sum);
    free(p);
    return 0;
}